﻿/**
 * Created in 2012-2-15.
 */
package kit4j.tests.poi;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hpbf.HPBFDocument;
import org.apache.poi.hpbf.extractor.PublisherTextExtractor;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlException;

/**
 * 
 * @author MiniKnife
 *
 */
public class TestConvertText {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws OpenXML4JException 
	 * @throws XmlException 
	 * @throws InvalidFormatException 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException, XmlException, OpenXML4JException {
//		System.out.println(xlsxToText(new FileInputStream("F:/1.xlsx")));
//		System.out.println(docxToText(new FileInputStream("F:/1.docx")));
//		System.out.println(pdfToText(new FileInputStream("F:/1.pdf")));
		System.out.println(pptToText(new FileInputStream("F:/1.ppt")));
	}
	
	static String docToText(InputStream in) throws IOException {
		XWPFWordExtractor extractor = new XWPFWordExtractor(new XWPFDocument(in));
		
		return extractor.getText();
	}
	
	static String docxToText(InputStream in) throws IOException {
		XWPFWordExtractor extractor = new XWPFWordExtractor(new XWPFDocument(in));
		
		return extractor.getText();
	}
	
	static String xlsToText(InputStream in) throws IOException {
		HSSFWorkbook book = new HSSFWorkbook(in);
		ExcelExtractor extractor = new ExcelExtractor(book);
		
		return extractor.getText();
	}
	
	static String xlsxToText(InputStream in) throws IOException {
		XSSFExcelExtractor extractor = new XSSFExcelExtractor(new XSSFWorkbook(in));
		
		return extractor.getText();
	}
	
	static String pdfToText(InputStream in) throws IOException {
		PublisherTextExtractor extractor = new PublisherTextExtractor(new HPBFDocument(in));
		
		return extractor.getText();
	}
	
	static String pptToText(InputStream in) throws IOException, InvalidFormatException, XmlException, OpenXML4JException {
		PowerPointExtractor extractor = new PowerPointExtractor(in);
		
		return extractor.getText();
	}
	
	static String pptxToText(InputStream in) throws IOException, InvalidFormatException, XmlException, OpenXML4JException {
		XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(OPCPackage.open(in));
		
		return extractor.getText();
	}
	
}
